package cn.edu.ctbu.lablist.service.impl;

import cn.edu.ctbu.lablist.constant.REnum;
import cn.edu.ctbu.lablist.exception.RException;
import cn.edu.ctbu.lablist.repository.StudentRepository;
import cn.edu.ctbu.lablist.domain.Student;
import cn.edu.ctbu.lablist.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentimplService implements StudentService {

    @Autowired
    private StudentRepository studentRepository;



    public List<Student> findAll(){
        return studentRepository.findAll();
    }


    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }

    /**
     *分页，带过滤的查询
     * @param student
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student> student, Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }


    /**
     * 按id进行查询
     * @param id 主键，整数
     * @return
     */
    public Student findById(Integer id){


        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 按名字进行查询,Like
     * @param name 字符串
     * @return
     */
    public List<Student> findByName(String name){


        return studentRepository.findByNameLike(name);
    }
    /**
     * 按学号查询
     * @param no
     * @return
     */
    public List<Student> findByNo(String no){

        return studentRepository.findByNoLike(no);
    }

    /**
     * 按名字和密码查询
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameAndPassword(String name,String password){


        return studentRepository.findByNameAndPassword(name,password);
    }

    /**
     * 按学号和密码查询
     * @param no
     * @param password
     * @return
     */
    public List<Student> findByNoAndPassword(String no,String password){


        return studentRepository.findByNoAndPassword(no,password);
    }

    /**
     * 创建
     *
     * @param student
     * @return
     */
    public Student insert(Student student){
        studentRepository.save(student);
        return student;
    }
    public Student add(Student student){
        studentRepository.save(student);
        return student;
    }
    /**
     * 更新
     *
     * @param student
     * @return
     */
    public Student update(Student student){
        studentRepository.save(student);
        return student;
    }

    public void delete(Long id){
        Student student=new Student();
        student.setId(Math.toIntExact(id));

        studentRepository.delete(student);
    }

    public Student delete(Student student){
        studentRepository.delete(student);

        return student;
    }

    public Student getById(Long id) {
        Student student = studentRepository.findById(Math.toIntExact(id)).orElse(null);
        return student;
    }

    public Student validateUsernameAndPassword(String username,String password) throws Exception{
        List<Student> students = studentRepository.findByNo(username);
        if(students.size() > 0){
            //可能对password进行加密,但我们暂时不做处理
            Student student = students.get(0);
            if(student.getPassword().equals(password)){
                //成功
                return student;
            }else{
                throw new RException(REnum.LOGIN_ERR);
            }
        }else{
            throw new RException(REnum.LOGIN_ERR);
        }
    }

}
